package com.bjesc_b2c.app.appraiset.mvp.view.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.bjesc_b2c.app.appraiset.R;
import com.bjesc_b2c.app.appraiset.adapter.WaitCheckCarAdapter;
import com.bjesc_b2c.app.appraiset.base.BaseActivity;
import com.bjesc_b2c.app.appraiset.http.IApiActivityRequest;
import com.bjesc_b2c.app.appraiset.utils.CustomSharedPreferences;
import com.bjesc_b2c.app.appraiset.utils.LocalGroupSearch;
import com.liaoinstan.springview.container.DefaultHeader;
import com.liaoinstan.springview.widget.SpringView;
import com.rxretrofit_build.util.MapUtil;

import java.util.ArrayList;
import java.util.List;

import app.pinggushi.bjesc.com.baserecyclerview.adapter.MultiItemTypeAdapter;
import butterknife.Bind;
import butterknife.OnClick;
import master_bean.CarForBean;
import rxretrofit_base.model.KeyBean;
import rxretrofit_base.model.ShowType;
import rxretrofit_base.model.TokenBean;
import rxretrofit_base.net.BaseSubscriber;
import rxretrofit_base.net.RxRequest;

/**
 * Created by yidong on 2017/8/2.
 */

public class WaitCheckCarActivity extends BaseActivity implements  SpringView.OnFreshListener{
    @Bind(R.id.recycler_view)
    RecyclerView carfromTwoRecycler;
    @Bind(R.id.spring_view)
    SpringView springView;

    @Bind(R.id.tv_title)
    TextView titleTv;
    @Bind(R.id.ed_text)
    EditText searchEd;
    private boolean isShowDialog = true;
    private WaitCheckCarAdapter mAdapter;
    private List<CarForBean> carForBean = new ArrayList<>();
    private Intent intent;
    private LinearLayoutManager twoManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        intent = new Intent();
        CustomSharedPreferences.saveString(mContext,new MapUtil().put(KeyBean.REFRESH.valueOf(),"0").build(),KeyBean.FILE_NAME.valueOf());
        titleTv.setText("巡店待采集");
    }

    private void initViews() {
        if (mAdapter == null) {
            mAdapter = new WaitCheckCarAdapter(this, R.layout.item_dealer_info, carForBean);
            twoManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
            carfromTwoRecycler.setLayoutManager(twoManager);
            carfromTwoRecycler.setAdapter(mAdapter);
            mAdapter.setOnItemClickListener(twoListener);
        } else {
            mAdapter.refresh(carForBean);
        }
        springView.setListener(this);
        springView.setHeader(new DefaultHeader(mContext));
        springView.setType(SpringView.Type.FOLLOW);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_dealers_list;
    }

    @Override
    protected void initEventAndData() {
        searchEd.addTextChangedListener(textWatcher);
    }

    @Override
    protected void createPresenter() {
        requestCarMarket(this);
    }

    private MultiItemTypeAdapter.OnItemClickListener twoListener = new MultiItemTypeAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
            String dealerID = mAdapter.getDatas().get(position).getDealer_id();
            CustomSharedPreferences.saveString(mContext,new MapUtil().put(KeyBean.DEALER_ID.valueOf(),dealerID).build(),KeyBean.FILE_NAME.valueOf());
            intent.putExtra("dealer_id", dealerID);
            intent.putExtra("Title", mAdapter.getDatas().get(position).getName());
            intent.setClass(WaitCheckCarActivity.this, DealerWaitCheckActivity.class);
            startIntent(intent, false);
        }

        @Override
        public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
            return false;
        }
    };

    @OnClick(R.id.iv_return)
    public void onClick() {
        finish();
    }
    @Override
    protected void onResume() {
        super.onResume();
        if(CustomSharedPreferences.getString(this, KeyBean.REFRESH.valueOf(), KeyBean.FILE_NAME.valueOf()).equals("1")){
            springView.callFresh();
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onRefresh() {
        isShowDialog = false;
        requestCarMarket(this);
    }

    @Override
    public void onLoadmore() {

    }

    public void requestCarMarket(final Context context) {
        RxRequest.request(ShowType.DISMISS, RxRequest.getClient(context, new MapUtil().build()).create(IApiActivityRequest.class).getMyDealers(),
                new BaseSubscriber<TokenBean<List<CarForBean>>>(context) {
                    @Override
                    public void onSuccess(TokenBean<List<CarForBean>> o) {
                        if (o.getData() != null && o.getData().size() > 0) {
                            carForBean = o.getData();
                            initViews();
                        }else{
                            carfromTwoRecycler.setAdapter(null);
                        }
                    }

                    @Override
                    public void onFailure(Throwable e) {
                        cancelLoading();
                        springView.onFinishFreshAndLoad();
                    }

                    @Override
                    public void onStart() {
                        if (isShowDialog) {
                            showLoading();
                        }
                    }

                    @Override
                    public void onCompleted() {
                        cancelLoading();
                        springView.onFinishFreshAndLoad();
                    }
                });
    }

    TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int arg1, int arg2, int arg3) {
            if (s != null && s.length() > 0 ) {
                List<CarForBean> listG = LocalGroupSearch.searchGroup(s, (ArrayList<CarForBean>) carForBean);
                if (listG != null && listG.size() > 0) {
                    if (mAdapter != null)
                        mAdapter.refresh(listG);
                } else {
                    showToast("搜索为空");
                }
            } else {
                if (mAdapter != null)
                    mAdapter.refresh(carForBean);
            }
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

        }

        @Override
        public void afterTextChanged(Editable arg0) {
        }
    };
}
